package com.chuangyang.fixboxlib.widgets;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuangyang.fixboxlib.R;

import java.io.IOException;

/**
 * Created by vanshu on 15/4/11 16:12.
 */
public class MessageBar extends LinearLayout implements View.OnClickListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {
    private ImageView radioPlay;
    private TextView radioTime;
    private AnimationDrawable frameAnimation;
   // private TimerHandler timerHandler = new TimerHandler(this);
    private String srcUrl;
    private int duration;
    private MediaPlayer mediaPlayer = null;
//    private int times;
//    private Timer timer;
//    private TimerTask timerTask;


    public MessageBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.message_bar, this, true);
        radioPlay = (ImageView) view.findViewById(R.id.radio_play);
        radioTime = (TextView) view.findViewById(R.id.radio_time);
        frameAnimation = (AnimationDrawable) radioPlay.getBackground();
        frameAnimation.setOneShot(false);
        setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            try {
                mediaPlayer.setDataSource(srcUrl);
                mediaPlayer.setScreenOnWhilePlaying(true);
                mediaPlayer.prepareAsync();
                mediaPlayer.setOnPreparedListener(this);
                mediaPlayer.setOnCompletionListener(this);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if(mediaPlayer.isPlaying()){
            stop();
        }
    }


    public void setRadioSrc(String srcUrl, int duration) {
        this.srcUrl = srcUrl;
        this.duration = duration;
    //    times = duration;
        radioTime.setText(duration + "\"");
    }

    public void stop() {
        reset();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        reset();
    }

    private void reset() {
        if (mediaPlayer != null) {
            mediaPlayer.release();
        }
        mediaPlayer = null;
        frameAnimation.stop();
   //     times = duration;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        frameAnimation.start();
        mediaPlayer.start();
//        timer = new Timer();
//        timerTask = new MyTask();
//        timer.schedule(timerTask, 0, 1000);
    }

//    private class MyTask extends TimerTask {
//        @Override
//        public void run() {
//            Message message = new Message();
//            message.arg1 = times;
//            message.what = 0;
//            timerHandler.sendMessage(message);
//            times--;
//            if (times == 0) {
//                timerTask.cancel();
//                timer.cancel();
//                timer = null;
//                timerHandler.sendEmptyMessage(1);
//            }
//        }
//    }
//
//    static class TimerHandler extends Handler {
//        private final WeakReference<MessageBar> mWeakreference;
//
//        TimerHandler(MessageBar messageBar) {
//            mWeakreference = new WeakReference<>(messageBar);
//        }
//
//        @Override
//        public void handleMessage(Message msg) {
//            MessageBar messageBar = mWeakreference.get();
//            switch (msg.what) {
//                case 0:
//                    if (messageBar != null) {
//                        messageBar.setText(msg.arg1);
//                    }
//                    break;
//                case 1:
//                    if (messageBar != null) {
//                        messageBar.reset();
//                    }
//                    break;
//            }
//        }
//    }

    public void setText(int duration) {
        radioTime.setText(duration + "\"");
    }
}
